package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.po.MemberPo;
import com.atguigu.crowd.entity.po.MemberPoExample;
import com.atguigu.crowd.mapper.MemberPoMapper;
import com.atguigu.crowd.service.api.MemberService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 在类上使用@Transactional(readOnly = true)针对查询操作设置事务属性
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberPoMapper memberPoMapper;

    @Override
    public MemberPo getMemberPOByLoginAcct(String loginacct) {

        // 1.创建 Example 对象
        MemberPoExample memberPoExample = new MemberPoExample();
        // 2.创建 Criteria 对象
        MemberPoExample.Criteria criteria = memberPoExample.createCriteria();
        // 3.封装查询条件
        criteria.andLoginacctEqualTo(loginacct);

        // 4.执行查询
        List<MemberPo> memberPoList = memberPoMapper.selectByExample(memberPoExample);

//        if (memberPoList == null || memberPoList.size()==0){
//            return null;
//        }
        // 5.获取结果
        return memberPoList.get(0);
    }

    @Transactional(propagation =  Propagation.REQUIRES_NEW,rollbackFor = Exception.class,readOnly = false)
    @Override
    public void saveMember(MemberPo memberPo) {
        System.out.println(11111);
        memberPoMapper.insertSelective(memberPo);
    }
}
